<?php 
	/**
订单确认控制器 @auth YWH 2016-11-22
	 */
	namespace Home\Controller;
	use Think\Controller;

	 class FillorderController extends Controller
	 {
	 	
	 	public function index()
	 	{
	 		$cart  	= M('cart');
	 		$data 	= $cart	->field('ct.num as anum,ct.good_id,sg.sku_id,pro.prodectsname,sg.logo,sg.price,pro.max_id,st.attr_value' )   ->alias('ct')
			    			->join('__SKU_GOODS__ sg ON sg.sku_id = ct.good_id')
			    			->join('__PRODUCT__ pro ON pro.max_id = sg.proudct_id')
			    			->join('__SKU_CAT__ st ON st.sku_id = sg.sku_id' )
			    			->where('uid='.session('suser_id'))
			    			->select();
			    if (!empty($data)) 
			    {
			    	foreach ($data as $key => $value) {
				
				 	$total[] = $value['anum'] * $value['price'];
					}

					$totalprice = array_sum($total);
					$count		= count($data);
			    }

			  $address 	= M('address');

			  $data1	= $address->where('uid='.I('session.suser_id'))->select();

	
			$this->assign('totalprice',$totalprice);
			$this->assign('count',$count);

			$this->assign('data',$data);
			$this->assign('data1',$data1);
	 		$this->display();
	 	}



	 	public function insert()
	 	{

	 		if (empty($_GET['id'])) 
	 		{
	 			
	 			$this->error('您访问的页面不存在');
	 			exit();
	 		}

	 		$cart  	= M('cart');
	 		$data 	= $cart	->field('ct.num as anum,ct.good_id,sg.sku_id,pro.prodectsname,sg.logo,sg.price,pro.max_id,st.attr_value' )   ->alias('ct')
			    			->join('__SKU_GOODS__ sg ON sg.sku_id = ct.good_id')
			    			->join('__PRODUCT__ pro ON pro.max_id = sg.proudct_id')
			    			->join('__SKU_CAT__ st ON st.sku_id = sg.sku_id' )
			    			->where('uid='.session('suser_id'))
			    			->select();




		$addressM 	= M('address');

		$uid	 	= session('suser_id');

		$map['id']	= array('eq',$_GET['id']);


		$map['uid']	=array('eq',$uid);

		$data1 		= $addressM->where($map)->find();

		$order 		= M('order');

		 if (empty($data)||empty($data1['address']))
		 {
                
                $this->error('数据出错，稍后再试',U('Home/index/index'),2);
                exit;
         }


            $time 		= date('Y-m-d H:i:s',time());

            $time2 		= time();

            $order_id	= $time2.'789';


            // NSLog($time2.'789');

          	
                            
			foreach ($data as $key => $value) 
			{
				
				 $monery = $value['anum'] * $value['price'];

				 $data2[] = ['uid'=>$uid,'goods_id'=>$value['sku_id'],'num'=>$value['anum'],'address'=>$data1['address'],'atime'=>$time,'phone'=>$data1['phone'],'monery'=>$monery,'order_id'=>$order_id,'status'=>0];
			}

			// NSLog($data2);

			// exit();
		
			$i = 0;
			$d = 0;
			foreach ($data2 as $key => $value) 
			{
				$order->startTrans();

				$flag = $order->add($value);

					if($flag>0)
					{
					$i++;
					$order->commit();//成功则提交
						}else{
					$d++;
					$order->rollback();//不成功，则回滚

					}
			}

			if ($i>0) 
			{
					
					$this->success($i."个订单生成成功，".$d."个订单生成失败",U('Home/Order/order'));
				}else{

					 $this->error($i."个订单生成成功，".$d."个订单生成失败",U('Home/inde/index'),2);
				}
	 		

	 	}






}